package com.isp.org.server.interfaces.http.role;

import com.isp.common.model.vo.PageResult;
import com.isp.common.model.vo.Result;
import com.isp.org.server.application.facade.RoleFacade;
import com.isp.org.server.interfaces.model.role.vo.request.DeleteRoleUserRequest;
import com.isp.org.server.interfaces.model.role.vo.request.RoleCreateRequest;
import com.isp.org.server.interfaces.model.role.vo.request.RoleToUserRequest;
import com.isp.org.server.interfaces.model.role.vo.request.RoleUpdateRequest;
import com.isp.org.server.interfaces.model.role.vo.response.RoleInfoResponse;
import com.isp.org.server.interfaces.model.role.vo.response.RoleUserResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author linsy
 * @version 1.0
 * @created 2024/1/24 17:51
 */
@Api(tags = "角色接口")
@RestController
@RequestMapping("/admin/api/roles")
@RequiredArgsConstructor
@Slf4j
public class RoleController {

    private final RoleFacade roleFacade;

    @ApiOperation("分页查询角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page_num", value = "页码", dataType = "number"),
            @ApiImplicitParam(name = "page_size", value = "页记录数", dataType = "number"),
    })
    @GetMapping("")
    public Result<PageResult<RoleInfoResponse>> pageList(@RequestParam(name = "page_num", defaultValue = "1") int pageNum,
                                                         @RequestParam(name = "page_size", defaultValue = "10") int pageSize) {
        return Result.success(roleFacade.pageList(pageNum, pageSize));
    }

    @ApiOperation("后台管理查询所有角色")
    @GetMapping("/all")
    public Result<List<RoleInfoResponse>> findAll() {
        List<RoleInfoResponse> result = roleFacade.findAll();
        return Result.success(result);
    }

    @ApiOperation("创建角色")
    @PostMapping()
    public Result<Boolean> create(@RequestBody @Valid RoleCreateRequest request) {
        Boolean response = roleFacade.create(request);
        return Result.success(response);
    }

    @ApiOperation("更新角色")
    @PutMapping("/{id}")
    public Result<Boolean> update(@PathVariable Long id, @RequestBody @Valid RoleUpdateRequest request) {
        Boolean response = roleFacade.update(id, request);
        return Result.success(response);
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        roleFacade.delete(id);
        return Result.success("操作成功", true);
    }

    @ApiOperation("用户分配角色")
    @PostMapping("/granted")
    public Result<Boolean> setRoleToUser(@RequestBody @Valid RoleToUserRequest sysMenu) {
        roleFacade.setRoleToUser(sysMenu.getUserId(), sysMenu.getRoleIds());
        return Result.success("操作成功", true);
    }

    @ApiOperation("删除角色的用户")
    @DeleteMapping("/user")
    public Result<Boolean> deleteRoleUser(@RequestBody @Valid DeleteRoleUserRequest request) {
        roleFacade.deleteRoleUser(request.getUserId(), request.getRoleIds());
        return Result.success("操作成功", true);
    }

    @ApiOperation("查询角色用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page_num", value = "页码", dataType = "number"),
            @ApiImplicitParam(name = "page_size", value = "页记录数", dataType = "number"),
            @ApiImplicitParam(name = "id", value = "角色ID", dataType = "number"),
    })
    @GetMapping("/{id}/users")
    public Result<PageResult<RoleUserResponse>> roleUsers(@RequestParam(name = "page_num", defaultValue = "1") int pageNum,
                                                          @RequestParam(name = "page_size", defaultValue = "10") int pageSize,
                                                          @PathVariable("id") Long id) {
        return Result.success(roleFacade.roleUsers(id, pageNum, pageSize));
    }

}
